from sqlalchemy import select

from poker_bot.core.interfaces.review import ReviewReader
from poker_bot.core.models.dto import ReviewDTO
from poker_bot.database.converters.review import convert_db_model_to_review_dto
from poker_bot.database.exception_mapper import exception_mapper
from poker_bot.database.models import Review
from poker_bot.database.repositories.base import SQLAlchemyRepo


class ReviewReaderImpl(SQLAlchemyRepo, ReviewReader):
    @exception_mapper
    async def get_review_by_id(self, review_id: int) -> ReviewDTO | None:
        """Получаем отзыв по id"""

        review: Review | None = await self._session.scalar(
            select(Review).where(Review.id == review_id)
        )
        if not review:
            return None

        return convert_db_model_to_review_dto(review)

    @exception_mapper
    async def get_reviews(self) -> list[ReviewDTO]:
        """Получаем все отзывы"""

        res = await self._session.scalars(select(Review))
        reviews: list[Review] = list(res)

        return [convert_db_model_to_review_dto(review) for review in reviews]
